<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class LoginController extends Controller
{
    public function index()
    {
        return view('admin.login');
    }

    public function doLogin(Request $request)
    {
        $userName = $request->input('username');
        $password = $request->input('password1');
        $timestamp = $request->input('timestamp');

        $userInfo = User::where('username',$userName)->where('status','正常')->first();

        if(!$userInfo){
            return response()->json(['code'=>-100,'msg'=>'用户不存在']);
        }
        $salt = Redis::get('salt_'.$userName);

        if((string)$password === (string)(sha1($userInfo->password.$timestamp.$salt))){
            session(['uid'=>$userInfo->id]);
//            session('uid',$userInfo->id);
            return response()->json(['code'=>0,'msg'=>'登录成功']);
        }
        return response()->json(['code'=>-200,'msg'=>'用户名或密码错误']);
    }
}
